const { List, ListNode, List2Array } = require('./common')

/* 迭代 */
var removeElements = function (head, val) {
  let isDel = false
  let dummyHead = new ListNode(undefined, head)
  for (let p = dummyHead; p.next; p.next && !isDel && (p = p.next)) {
    if (p.next.val === val) {
      isDel = true
      p.next = p.next.next
    } else {
      isDel = false
    }
  }
  return dummyHead.next
};
var removeElements = function (head, val) {

  let dummyHead = new ListNode(undefined, head)
  let p = dummyHead
  while (p.next) {
    if (p.next.val === val) {
      p.next = p.next.next
    } else {
      p = p.next
    }
  }
  return dummyHead.next
};



/* 递归 */
removeElements = function (head, val) {
  if (head == null) {
    return head
  }
  head.next = removeElements(head.next, val)
  return head.val === val ? head.next : head
}

console.log(List2Array(removeElements(List([1, 2, 3, 6, 4, 5, 6]), 6)));